package com.jhinwins.equipmenteb.equipmentebserver.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jhinwins.equipmenteb.equipmentebserver.dao.EvaluateDao;
import com.jhinwins.equipmenteb.equipmentebserver.entity.EvaluateBean;
import com.jhinwins.equipmenteb.equipmentebserver.entity.ProductBean;
import com.jhinwins.equipmenteb.equipmentebserver.service.EvaluateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EvaluateServiceImpl implements EvaluateService {
    private EvaluateDao evaluateDao;

    @Autowired
    public EvaluateServiceImpl(EvaluateDao evaluateDao) {
        this.evaluateDao = evaluateDao;
    }

    @Override
    public JSONArray getEvaluates(Long productId) {
        JSONArray datas = new JSONArray();

        EvaluateBean evaluateBeanExa = new EvaluateBean();
        ProductBean productBean = new ProductBean();
        productBean.setId(productId);
        evaluateBeanExa.setProductBean(productBean);
        List<EvaluateBean> evaluateBeans = evaluateDao.findAll(Example.of(evaluateBeanExa));
        for (EvaluateBean evaluateBean : evaluateBeans) {
            JSONObject evaluate = new JSONObject();
            evaluate.put("username", evaluateBean.getUserBean().getNickname());
            evaluate.put("icon", evaluateBean.getUserBean().getIcon());
            evaluate.put("content", evaluateBean.getContent());
            evaluate.put("time", evaluateBean.getTime());
            evaluate.put("rate", evaluateBean.getLevel());
            datas.add(evaluate);
        }
        return datas;
    }
}
